package com.asimplefox.service.impl;

import com.asimplefox.entity.Role;
import com.asimplefox.mapper.RoleMapper;
import com.asimplefox.serurity.TokenManager;
import com.asimplefox.service.IRoleService;
import com.asimplefox.service.abst.ServiceImplAll;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author kloping
 * @since 2024-10-14
 */
@Service
public class RoleServiceImpl extends ServiceImplAll<RoleMapper, Role> implements IRoleService {
    @Override
    public Object role(Integer id) {
        List<Role> roles = this.lambdaQuery().eq(Role::getUserId, id).list();
        for (Role role : roles) {
            if (role.getUserRole().equals(TokenManager.ADMIN)) {
                if (this.removeByMap(Map.of("user_id", id, "user_role", TokenManager.ADMIN))) {
                    roles = this.lambdaQuery().eq(Role::getUserId, id).list();
                    return roles;
                }
            }
        }
        this.save(new Role().setUserId(id).setUserRole(TokenManager.ADMIN).setTimestamp(Long.MAX_VALUE));
        roles = this.lambdaQuery().eq(Role::getUserId, id).list();
        return roles;
    }
}
